package com.zhou.common.quartz.test.timewheel;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/**
 * @author xianfeng
 * @date 2023/12/16 9:52
 * @description: 定时任务项
 */


@Data
@Slf4j
public class TimerTaskEntry implements Comparable<TimerTaskEntry> {

    volatile TimerTaskList timedTaskList;
    TimerTaskEntry next;
    TimerTaskEntry prev;
    private TimerTask timerTask;
    private long expireMs;

    public TimerTaskEntry(TimerTask timedTask, long expireMs) {
        this.timerTask = timedTask;
        this.expireMs = expireMs;
        this.next = null;
        this.prev = null;
    }

    void remove() {
        TimerTaskList currentList = timedTaskList;
        while (currentList != null) {
            currentList.remove(this);
            currentList = timedTaskList;
        }
    }

    @Override
    public int compareTo(TimerTaskEntry o) {
        return ((int) (this.expireMs - o.expireMs));
    }

    @Override
    public String toString() {
        return "TimerTaskEntry{" +
                "timedTaskList=" + timedTaskList +
                ", timerTask=" + timerTask +
                ", expireMs=" + expireMs +
                '}';
    }
}